package ArrayList;

import java.util.Stack;

class Solution7 {
    public int serachMax(int[] nums,int left,int right){
        int max =0;
        int index = 0;
         for(left =0;left<right;++left){
             max = Math.max(max,nums[left]);
             index = left;
         }
         return index;
    }

    public TreeNode constructMaximumBinaryTree(int[] nums) {
        //需要不断地在某一段区间内寻找数组中的最大值，最大值的左边是左子树，右边是右子树
        //我们要分成两端区间来做
        Stack<Integer> stack = new Stack<>();
        stack.add(1);
        int x = stack.pop();
        int maxi = serachMax(nums,0,nums.length);//最开始的根节点
        TreeNode root = new TreeNode(nums[maxi]);
        //[0,maxi]   [maxi,nums.length-1]
        root.left = new TreeNode(serachMax(nums,0,maxi));
        root.right = new TreeNode(serachMax(nums,maxi,nums.length-1));
        return root;
    }
}